package com.itcast.demoone

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class PageTwoActivity : AppCompatActivity() {

    // 定义一个常量
    private val TAG = "itHeiMa-PageTwo"
    @SuppressLint("MissingInflatedId", "SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        Log.d(TAG, "+++++++onCreate")
        
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_page_two)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 2. 接收上一个Activity传递的参数
        // 一定要在生命周期的钩子函数中接收
        val userName = intent.getStringExtra("user_name")
        val userAge = intent.getIntExtra("user_age", 0)
        val userSex = intent.getStringExtra("user_sex")
        findViewById<TextView>(R.id.tv_text).text = "用户名：$userName\n年龄：$userAge\n性别：$userSex"

        // 1. 获取返回按钮
        val backButton = findViewById<View>(R.id.btn_back)
        backButton.setOnClickListener {

            // 1.1 创建一个Intent对象
            val intent = Intent().apply {
                putExtra("back_data", "我是给上一个页面的返回数据")
            }

            // 1.2 设置返回结果
            setResult(RESULT_OK, intent)


            finish()
        }
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "++++++onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "++++++onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "++++++onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "++++++onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "++++++onDestroy")
    }

    override fun onRestart() {
        super.onRestart()
        Log.d(TAG, "++++++onRestart")
    }
}